package com.ybxx.asset.action;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;

import com.ybxx.asset.model.Building;
import com.ybxx.asset.model.Company;
import com.ybxx.asset.model.Structures;
import com.ybxx.asset.model.Trench;
import com.ybxx.asset.model.vo.BuildingVO;
import com.ybxx.asset.model.vo.TrenchVO;
import com.ybxx.asset.service.CompanyService;
import com.ybxx.asset.service.StructuresService;
import com.ybxx.asset.service.TrenchService;
import com.ybxx.core.command.QueryFilter;
import com.ybxx.core.util.DateUtil;
import com.ybxx.core.util.ExportExcel;
import com.ybxx.core.util.UUIDGenerator;
import com.ybxx.core.web.action.BaseAction;

public class TrenchAction extends BaseAction {
	@Resource
	private CompanyService companyService;
	@Resource
	private TrenchService trenchService;
	private List<Company> companys;
	private List<Trench> trenchs;
	private List<TrenchVO> vos;
	private Trench trench;
	private String msg="succ";
	/*打印*/
	public String print(){
		trenchs=trenchService.exportExcel_Print(trench);
		vos=new ArrayList<TrenchVO>();
		if(trenchs!=null){
			int i=1;
			for(Trench trench:trenchs){
				TrenchVO vo=new TrenchVO();
				vo.setSeq(i+"");
				vo.setBookvalue(trench.getBookvalue());
				vo.setBuildmonth(trench.getBuildmonth());
				vo.setCname(trench.getCompany().getCname());
				vo.setContent(trench.getContent());
				vo.setMdept(trench.getMdept());
				vo.setNetbookvalue(trench.getNetbookvalue());
				vo.setTlen(trench.getTlen());
				vo.setTname(trench.getTname());
				vo.setZcbh(trench.getZcbh());
				vos.add(vo);
				i++;
			}
		}
		return "print";
	}
	/*导出*/
	public String exportExcel(){
		trenchs=trenchService.exportExcel_Print(trench);
		vos=new ArrayList<TrenchVO>();
		if(trenchs!=null){
			int i=1;
			for(Trench trench:trenchs){
				TrenchVO vo=new TrenchVO();
				vo.setSeq(i+"");
				vo.setBookvalue(trench.getBookvalue());
				vo.setBuildmonth(trench.getBuildmonth());
				vo.setCname(trench.getCompany().getCname());
				vo.setContent(trench.getContent());
				vo.setMdept(trench.getMdept());
				vo.setNetbookvalue(trench.getNetbookvalue());
				vo.setTlen(trench.getTlen());
				vo.setTname(trench.getTname());
				vo.setZcbh(trench.getZcbh());
				vos.add(vo);
				i++;
			}
		}
		ExportExcel<TrenchVO> ex=new ExportExcel<TrenchVO>();
		OutputStream out;
		try {
			out = super.getResponse().getOutputStream();
			super.getResponse().reset();// 清空输出流
			super.getResponse().setContentType("text/html; charset=UTF-8");
			super.getResponse().setHeader("Content-disposition",
					"attachment; filename="+DateUtil.formatDateYYYYMMdd(new Date())+".xls");// 设定输出文件头
			super.getResponse().setContentType("application/msexcel");// 定义输出类型
			String[] headers={"序号","企业名称","资产编号","名称","建成年月","长度","账面原值","账面净值","管理（使用）部门","备注"};
	        ex.exportExcel("管沟信息",headers, vos, out);
	        out.flush();
	        out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	/*删除*/
	public String remove(){
		try{
			trenchService.remove(trench.getTid());
		}catch(Exception ex){
			msg="管沟信息删除失败";
		}finally{
			return "succ";
		}
	}
	/*加载查看时候需要展示的数据*/
	public String view(){
		if(trench!=null && StringUtils.isNotEmpty(trench.getTid())){
			trench=trenchService.get(trench.getTid());
		}
		return "view";
	}
	public String init(){
		companys=companyService.getAll();
		return "init";
	}
	public String queryPage(){
		QueryFilter filter=new QueryFilter(getRequest());
		trenchs=trenchService.queryPage(trench,filter);
		super.setRownum(filter.getPagingBean().getTotalItems());
		super.setCurrentpage(Integer.parseInt(filter.getPagingBean().getStart()+"")/Integer.parseInt(filter.getPagingBean().getPageSize()+"")+1);
		super.setPageSize(Integer.parseInt(filter.getPagingBean().getPageSize()+""));
		return "queryPage";
	}
	/*保存新增，修改信息*/
	public String save(){
		if(StringUtils.isEmpty(trench.getTid())){
			trench.setTid(UUIDGenerator.getUUID());
		}
		try{
			trenchService.save(trench);
		}catch(Exception ex){
			msg="管沟信息保存失败.";
		}finally{
			return "succ";
		}
	}
	/*加载修改时候需要展示的数据*/
	public String load(){
		companys=companyService.getAll();
		if(trench!=null && StringUtils.isNotEmpty(trench.getTid())){
			trench=trenchService.get(trench.getTid());
		}
		return "load";
	}
	public List<Company> getCompanys() {
		return companys;
	}
	public void setCompanys(List<Company> companys) {
		this.companys = companys;
	}
	public List<Trench> getTrenchs() {
		return trenchs;
	}
	public void setTrenchs(List<Trench> trenchs) {
		this.trenchs = trenchs;
	}
	public Trench getTrench() {
		return trench;
	}
	public void setTrench(Trench trench) {
		this.trench = trench;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public List<TrenchVO> getVos() {
		return vos;
	}
	public void setVos(List<TrenchVO> vos) {
		this.vos = vos;
	}
}
